package nc.ui.budget.useinfo;

import java.io.*;
import java.math.BigDecimal;
import nc.bs.framework.common.RuntimeEnv;
import nc.bs.logging.Logger;
import nc.ui.ml.NCLangRes;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UITable;
import nc.ui.pub.querymodel.UIUtil;
import nc.vo.pub.lang.UFDouble;

import org.apache.poi.hssf.usermodel.*;

public class UseinfoExcelUtil
{

    public UseinfoExcelUtil()
    {
    }

    public static HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook)
    {
        HSSFFont headerFont = workbook.createFont();
        headerFont.setBoldweight((short)700);
        headerFont.setColor((short)9);
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setAlignment((short)2);
        headerStyle.setVerticalAlignment((short)1);
        headerStyle.setFont(headerFont);
        headerStyle.setFillForegroundColor((short)48);
        headerStyle.setFillPattern((short)1);
        headerStyle.setWrapText(true);
        return headerStyle;
    }

    public static void makeHeader(HSSFWorkbook workbook, HSSFSheet sheet, UITable dbTable)
    {
        int tableColNum = dbTable.getColumnCount();
        int totalColNum = tableColNum;
        HSSFCellStyle headerStyle = getHeaderStyle(workbook);
        HSSFRow headerRow = sheet.createRow(0);
        for(int j = 0; j < totalColNum; j++)
        {
            String colName = dbTable.getColumnName(j);
            HSSFCell cell = headerRow.createCell((short)j);
            cell.setCellValue(new HSSFRichTextString(colName));
            cell.setCellStyle(headerStyle);
        }

    }

    public  void export2Excel(File file, UITable dbTable, String title)
    {
        FileOutputStream out;
        HSSFWorkbook workbook;
        String filename;
        int tableColNum = dbTable.getColumnCount();
        int totalColNum = tableColNum;
        int iBodyRowCount = dbTable.getRowCount();
        int maxColWidth[] = new int[totalColNum];
        out = null;
        int linecount = 0;
        Object cellObj = null;
        String cellObjStr = null;
        workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet(title);
        HSSFRow bodyRows[] = new HSSFRow[iBodyRowCount];
        makeHeader(workbook, sheet, dbTable);
        HSSFCellStyle bodyStyle[] = new HSSFCellStyle[tableColNum];
        for(int i = 0; i < tableColNum; i++)
            bodyStyle[i] = workbook.createCellStyle();

        for(int i = 0; i < iBodyRowCount; i++)
        {
            bodyRows[i] = sheet.createRow(linecount + 1);
            for(int j = 0; j < totalColNum; j++)
            {
                cellObj = dbTable.getValueAt(i, j);
                if(cellObj == null)
                    continue;
                cellObjStr = cellObj.toString();
                HSSFCell cell = bodyRows[i].createCell((short)j);
                if(cellObj instanceof Integer)
                    cell.setCellValue(Double.parseDouble(cellObj.toString()));
                else
                if(cellObj instanceof UFDouble)
                    cell.setCellValue(((UFDouble)cellObj).doubleValue());
                else
                if(cellObj instanceof Double)
                    cell.setCellValue(((Double)cellObj).doubleValue());
                else
                if(cellObj instanceof BigDecimal)
                    cell.setCellValue(((BigDecimal)cellObj).doubleValue());
                else
                    cell.setCellValue(new HSSFRichTextString(cellObj.toString()));
                maxColWidth[j] = UIUtil.getMaxColWidth(cellObjStr, dbTable, null, sheet.getColumnWidth((short)j));
                cell.setCellStyle(bodyStyle[j]);
            }

            linecount++;
        }

        for(int i = 0; i < maxColWidth.length; i++)
        {
            if(maxColWidth[i] < 20)
                maxColWidth[i] = 20;
            short sTemp = (short)((maxColWidth[i] + 5) * 50);
            sheet.setColumnWidth((short)i, sTemp);
        }

        filename = file.getAbsolutePath();
        try {
			out = new FileOutputStream(filename);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			workbook.write(out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        String hintMsg = NCLangRes.getInstance().getStrByID("10241201", "UPP10241201-001337");
        if(!RuntimeEnv.getInstance().isRunningInServer())
            MessageDialog.showHintDlg(dbTable, NCLangRes.getInstance().getStrByID("10241201", "UPP10241201-000099"), hintMsg);
       
        if(out!=null)
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    }
}
